package main

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
	"time"
)


//https://github.com/mongodb/mongo-go-driver#installation
//https://www.cnblogs.com/zcqkk/p/11234227.html
func main()  {
	client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://192.168.2.48:27017"))
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	err = client.Connect(ctx)   // 连接数据库
	defer func() {
		if err = client.Disconnect(ctx); err != nil {
			panic(err)
		}
	}()

	// 获取数据库和集合
	collection := client.Database("count").Collection("im_chat_click")

	var result struct {
		Uid int32
		Teacherid int32
		Platform string
	}
	filter := bson.M{"cid": "2735"}
	ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	err = collection.FindOne(ctx, filter).Decode(&result)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result)

}